import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

contextBridge.exposeInMainWorld('electronAPI', {
  getLeimu: () => ipcRenderer.invoke('get-leimu'),
  getDiqu: () => ipcRenderer.invoke('get-diqu'),
  getCurrentTheme: () => ipcRenderer.invoke('get-current-theme'),
  addUser: (name, email) => ipcRenderer.invoke('add-user', { name, email }),
  editUser: (name, pwd, id) => ipcRenderer.invoke('edit-user', { name, pwd, id }),
  getUsers: (page, pageSize, keyword) =>
    // 返回Promise以便调用者可以获取数据
    ipcRenderer.invoke('get-users', {
      page: page || 1, // 当前页码
      pageSize: pageSize || 10, // 每页记录数
      keyword: keyword || '' // 查询关键字，确保即使没有传入也不会是undefined
    }),
  downTemplate: () => ipcRenderer.invoke('down-template'),
  importShop: () => ipcRenderer.invoke('import-shop'),
  // 打开文件选择器并返回文件路径
  chooseFile: () => ipcRenderer.invoke('choose-file'),
  // 模板
  addTemp: (name, str) => ipcRenderer.invoke('add-temp', { name, str }),
  editTemp: (id, name, str) => ipcRenderer.invoke('edit-temp', { id, name, str }),
  getTemps: (page, pageSize, keyword) =>
    ipcRenderer.invoke('get-temps', {
      page: page || 1, // 当前页码
      pageSize: pageSize || 10, // 每页记录数
      keyword: keyword || '' // 查询关键字，确保即使没有传入也不会是undefined
    }),
  // 任务
  addTask: (path, name, mbs, shops) => ipcRenderer.invoke('add-task', { path, name, mbs, shops }),
  editTask: (id, name, str) => ipcRenderer.invoke('edit-task', { id, name, str }),
  getTasks: (page, pageSize, keyword) =>
    ipcRenderer.invoke('get-tasks', {
      page: page || 1, // 当前页码
      pageSize: pageSize || 10, // 每页记录数
      keyword: keyword || '' // 查询关键字，确保即使没有传入也不会是undefined
    }),
  getLogin: () => ipcRenderer.invoke('get-login'),
  getAllMbs: () => ipcRenderer.invoke('get-all-mbs'),
  // 启动任务
  startTask: (taskId) => ipcRenderer.invoke('start-task', { taskId }),
  stopTask: (taskId) => ipcRenderer.invoke('stop-task', { taskId }),
  getUserTask: (uid, task_id, page = 1, pageSize = 10) =>
    ipcRenderer.invoke('get-usertask', {
      uid,
      task_id,
      page: page || 1, // 当前页码
      pageSize: pageSize || 10 // 每页记录数
    }),
  // 配置相关API
  saveConfig: (key, value) => {
    ipcRenderer.invoke('save-config', key, value)
  },
  loadConfig: (key) => ipcRenderer.invoke('load-config', key),
  // 文件操作API
  readFile: (filePath) => ipcRenderer.invoke('read-file', filePath),
  saveFile: (filePath, content) => ipcRenderer.invoke('save-file', filePath, content),
  settingYfmb: (shop_id, yfmb) => ipcRenderer.invoke('setting-yfmb', { shop_id, yfmb })
})

// Custom APIs for renderer
const api = {
  onNavigate: (callback) => {
    ipcRenderer.on('navigate', (event, path) => callback(path))
  },
  onShowHello: (callback) => {
    ipcRenderer.on('show-hello', () => callback())
  },
  onThemeChanged: (callback) => {
    ipcRenderer.on('theme-changed', (event, isDarkMode) => callback(isDarkMode))
  },
  setDarkMode: (isDarkMode) => {
    ipcRenderer.send('set-dark-mode', isDarkMode)
  },
  // 文件解析相关API
  parseTxtFile: (lines) => {
    ipcRenderer.send('parse-txt-file', { lines })
  },
  onTxtFileParsed: (callback) => {
    ipcRenderer.on('txt-file-parsed', (event, data) => callback(data))
  },
  onTbcookie: (callback) => {
    ipcRenderer.on('tbcookie', (event, cookies) => callback(cookies))
  },
  // 监听淘宝数据事件
  onTbdata: (callback) => {
    ipcRenderer.on('tb-data', (event, data) => callback(data))
  },
  onSetting: (callback) => {
    ipcRenderer.on('setting', (event, data) => callback(data))
  }
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}
